from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from .models import registryInfo
from .models import robot_dtalk


class RegistryDetailSerializer(serializers.ModelSerializer):
    '''仓库信息'''

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", help_text="生成日期")
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", help_text="更新日期")

    class Meta:
        model = registryInfo
        fields = "__all__"


class RegistryCreateSerializer(serializers.HyperlinkedModelSerializer):
    '''
    更新仓库信息
    '''
    env = serializers.CharField(max_length=20, required=True, help_text="环境")
    registry = serializers.CharField(max_length=200, required=True, help_text="仓库地址")
    namespace = serializers.CharField(max_length=20, required=True, help_text="命名空间")
    username = serializers.CharField(max_length=100, required=False, help_text="仓库帐号")
    password = serializers.CharField(max_length=100, required=False, help_text="仓库密码")

    class Meta:
        '''
        验证仓库是否已存在
        '''
        model = registryInfo
        fields = ("env", "registry", "namespace", "username", "password", "label")
        validators = [
            UniqueTogetherValidator(
                queryset=registryInfo.objects.all(),
                fields=("env", "registry", "namespace"),
                message="仓库已存在"
            )
        ]


class DtalkDetailSerializer(serializers.ModelSerializer):
    '''机器人列表'''

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", help_text="创建日期")
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", help_text="更新日期")

    class Meta:
        model = robot_dtalk
        fields = "__all__"


class DtalkCreateSerializer(serializers.ModelSerializer):
    '''创建机器人信息'''

    class Meta:
        model = robot_dtalk
        fields = ('robot_name', 'webhook')

        validators = [
            UniqueTogetherValidator(
                queryset=robot_dtalk.objects.all(),
                fields=('robot_name', "webhook"),
                message="机器人已存在"
            )
        ]
